package com.shycloud.mido.organ.ranking;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shycloud.mido.organ.api.entity.WorksEntity;
import com.shycloud.mido.organ.api.vo.RankingVO;
import com.shycloud.mido.organ.mapper.MemberMapper;
import com.shycloud.mido.organ.mapper.WorksMapper;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

@Component("type1")
@AllArgsConstructor
public class RankingWorksCount extends Ranking {

	/** 用户模块 mapper */
	private MemberMapper memberMapper;
	/** 作品模块 mapper */
	private WorksMapper worksMapper;

	/**
	 * 获取作品数
	 *
	 * @param username
	 * @return
	 */
	@Override
	public IPage<RankingVO> getRankingList(String username, Page page) {

		IPage<RankingVO> finalResult;

		//所有用户
		if (StringUtils.isEmpty(username)) {
			finalResult = worksMapper.getWorksCountList(page);
		}

		//指定用户
		else {
			finalResult = getOneUserWorks(username, memberMapper, worksMapper, page);
		}

		//List<RankingVO> rankingVOS = finalResult.getRecords();

//		for (RankingVO rankingVO : rankingVOS) {
//
//			String memberId = rankingVO.getMemberId();
//			//作品数
//			Integer count = worksMapper.selectCount(new QueryWrapper<WorksEntity>().eq("member_id", memberId));
//
//			rankingVO.setCount(count);
//		}
//
//		List<RankingVO> collect = rankingVOS.stream().sorted((x, y) -> y.getCount() - x.getCount()).collect(Collectors.toList());
		//finalResult.setRecords(collect);
		return finalResult;
	}
}
